﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using DashFire;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;

public class ResDeployer
{
  #region BuildPackage
  public static bool BuildPackage()
  {
    string resDir = ResBuildHelper.GetFilePathAbs(ResBuildHelper.GetPlatformPath(ResBuildConfig.BuildOptionTarget));
    if (!System.IO.Directory.Exists(resDir)) {
      ResBuildLog.Warn("ResDeployer.BuildPackage failed srcPath:" + resDir);
      return false;
    }
    string packageDir = ResBuildHelper.GetFilePathAbs(ResBuildHelper.GetPackagePath(ResBuildConfig.BuildOptionTarget));
    if (System.IO.Directory.Exists(packageDir)) {
      AssetDatabase.MoveAssetToTrash(ResBuildHelper.GetPackagePath(ResBuildConfig.BuildOptionTarget));
    }
    System.IO.Directory.CreateDirectory(packageDir);

    string packagePath = ResBuildHelper.GetFilePathAbs(ResBuildHelper.FormatResPackageFilePath());
    string packageDirName = Path.GetDirectoryName(packagePath);
    if (!System.IO.Directory.Exists(packageDirName)) {
      System.IO.Directory.CreateDirectory(packageDirName);
    }
    if (System.IO.File.Exists(packagePath)) {
      File.Delete(packagePath);
    }
    string[] exclude = new string[] {
      "ServerVersion.txt",
      ".meta",
      "ResBuildConfig_ab.txt",
      "ResBuildDiff_ab.txt",
      "ClientVersion.txt"
    };

    ZipHelper.ZipDir(resDir, packagePath, exclude);

    string versionPathSource = Path.Combine(resDir, ResBuildConfig.VersionServerFile);
    string versionPathDest = Path.Combine(packageDir, ResBuildConfig.VersionServerFile);
    if (!File.Exists(versionPathSource)) {
      ResBuildLog.Warn("ResDeployer.CopyBuildInResources copy file not exist. versionPathSource:" + versionPathSource);
      return false;
    }
    File.Copy(versionPathSource, versionPathDest, true);
    ResBuildLog.Info("ResDeployer.CopyBuildInResources copy ab from:{0} to:{1}",
      versionPathSource, versionPathDest);

    AssetDatabase.Refresh();
    ResBuildLog.Info("ResDeployer.CopyBuildInResources Done");
    return true;
  }
  #endregion

  #region Commit
  public static bool CommitBuildInResources()
  {
    CleanBuildInRes();
    CleanStreamingAssets();
    string srcDir = ResBuildHelper.GetFilePathAbs(ResBuildHelper.GetPlatformPath(ResBuildConfig.BuildOptionTarget));
    if (!System.IO.Directory.Exists(srcDir)) {
      ResBuildLog.Warn("ResDeployer.CopyBuildInResources failed srcPath:" + srcDir);
      return false;
    }
    string destDir = ResBuildHelper.GetFilePathAbs(ResBuildConfig.ResCommitBuildInPath);
    if (!System.IO.Directory.Exists(destDir)) {
      System.IO.Directory.CreateDirectory(destDir);
    }
    if (!System.IO.Directory.Exists(destDir)) {
      ResBuildLog.Warn("ResDeployer.CopyBuildInResources failed destDir:" + destDir);
      return false;
    }

    string resSheetPathSource = Path.Combine(srcDir, ResBuildConfig.ResSheetZipPath);
    string resSheetPathDest = Path.Combine(destDir, ResBuildConfig.ResSheetZipPath);
    if (!File.Exists(resSheetPathSource)) {
      ResBuildLog.Warn("ResDeployer.CopyBuildInResources copy file not exist. resSheetPathSource:" + resSheetPathSource);
      return false;
    }
    File.Copy(resSheetPathSource, resSheetPathDest, true);
    ResBuildLog.Info("ResDeployer.CopyBuildInResources copy ab from:{0} to:{1}",
      resSheetPathSource, resSheetPathDest);

    string versionPathSource = ResBuildHelper.GetFilePathAbs(ResBuildHelper.FormatVersionServerFilePath());
    string versionPathDest = Path.Combine(destDir, ResBuildConfig.VersionClientFile);
    if (!File.Exists(versionPathSource)) {
      ResBuildLog.Warn("ResDeployer.CopyBuildInResources copy file not exist. versionPathSource:" + versionPathSource);
      return false;
    }
    File.Copy(versionPathSource, versionPathDest, true);
    ResBuildLog.Info("ResDeployer.CopyBuildInResources copy ab from:{0} to:{1}",
      versionPathSource, versionPathDest);

    AssetDatabase.Refresh();
    ResBuildLog.Info("ResDeployer.CopyBuildInResources Done");
    return true;
  }
  #endregion
  #region Clean
  public static bool CleanCache()
  {
    bool ret = false;
    try {
      Caching.CleanCache();
      string resCachePath = Path.Combine(Application.persistentDataPath, ResBuildConfig.ResCommitCachePath);
      if (Directory.Exists(resCachePath)) {
        ResBuildHelper.DeleteDirectory(resCachePath);
      }
      string resSheetPath = Path.Combine(Application.persistentDataPath, ResBuildConfig.ResSheetCachePath);
      if (Directory.Exists(resSheetPath)) {
        ResBuildHelper.DeleteDirectory(resSheetPath);
      }
      ret = true;
      ResBuildLog.Info("ResBuildProcesser.CleanCache Done");
    } catch (System.Exception ex) {
      ResBuildLog.Warn("ResBuildProcesser.CleanCache failed! ex:" + ex);
      ret = false;
    }
    return ret;
  }
  public static bool CleanBuildInRes()
  {
    string buildinPath = ResBuildHelper.GetFilePathAbs(ResBuildConfig.ResCommitBuildInPath);
    if (Directory.Exists(buildinPath)) {
      ResBuildHelper.DeleteDirectory(buildinPath);
    }
    ResBuildLog.Info("ResBuildProcesser.CleanBuildInRes Done");
    return true;
  }
  public static bool CleanStreamingAssets()
  {
    string[] tStreamingAssetDirs = ResBuildConfig.ResStreamingAssetPath.Split(ResBuildConfig.ConfigSplit,
      StringSplitOptions.RemoveEmptyEntries);
    if (tStreamingAssetDirs != null && tStreamingAssetDirs.Length > 0) {
      foreach (string saDir in tStreamingAssetDirs) {
        AssetDatabase.MoveAssetToTrash(saDir);
      }
    }
    return true;
  }
  public static bool CleanOutputDir()
  {
    string outputPath = ResBuildHelper.GetFilePathAbs(ResBuildConfig.ResBuildConfigOutputPath);
    if (System.IO.Directory.Exists(outputPath)) {
      ResBuildHelper.DeleteDirectory(outputPath);
    }
    ResBuildLog.Info("ResProcess.CleanOutputDir Done");
    return true;
  }
  public static bool CleanAll()
  {
    if (ResDeployer.CleanCache()
        && ResDeployer.CleanBuildInRes()
        && ResDeployer.CleanStreamingAssets()
        && ResDeployer.CleanOutputDir()) {
      return true;
    }
    return false;
  }
  public static bool CleanOutputResDir()
  {
    string outputPath = ResBuildHelper.GetFilePathAbs(ResBuildHelper.GetPackagePath(ResBuildConfig.BuildOptionTarget));
    if (System.IO.Directory.Exists(outputPath)) {
      ResBuildHelper.DeleteDirectory(outputPath);
    }
    ResBuildLog.Info("ResProcess.CleanOutputResDir Done");
    return true;
  }
  #endregion
}
